package com.wegis.demo.service.impl;

import com.wegis.demo.entity.Geom;
import com.wegis.demo.mapper.GeomMapper;
import com.wegis.demo.service.IGeomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author slc
 * @since 2021-07-25
 */
@Service
public class GeomServiceImpl extends ServiceImpl<GeomMapper, Geom> implements IGeomService {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public Map<String, Object> addGeom(Geom geom) {
        Map<String, Object> mapResult = new HashMap<>();
        String sql = String.format("insert into geom (geom) values (ST_SetSRID(ST_GeomFromGeoJSON('%s'),%d))",
                geom.getGeom(),4326);
        System.out.println(sql);
        try {
            jdbcTemplate.execute(sql);
        } catch (Exception e) {
            mapResult.put("status", 201);
            mapResult.put("msg", "插入geometry失败");
            return mapResult;
        }

        mapResult.put("status", 200);
        mapResult.put("msg", "插入geometry成功");
        return mapResult;
    }
}
